package de.upb.messerschmidt.set;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import de.upb.messerschmidt.set.entity.Event;
import de.upb.messerschmidt.set.entity.EventStatus;
import de.upb.messerschmidt.set.entity.Person;
import de.upb.messerschmidt.set.jdo.PMF;
import de.upb.messerschmidt.set.util.Utils;

/**
 * This Servlet returns the {@link Attendee} status for a specific user and status. If
 * history is provided as true it will only return {@link Event}s in the past.
 * 
 * @author Tim Messerschmidt
 * 
 */
public class GetAttendEventServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String username = req.getParameter("user");
		String status = req.getParameter("status");
		String history = req.getParameter("history");

		if (username != null) {
			if (status != null && history == null)
				history = "false";

			getAttendingHistory(resp, username, status, history);
		}
	}

	@SuppressWarnings("unchecked")
	private void getAttendingHistory(HttpServletResponse resp, String username,
			String status, String history) throws IOException {
		Person person = null;

		String[] name = Utils.nameParser(username);

		PersistenceManager pm = PMF.get().getPersistenceManager();

		// Query for the User
		Query attendingHistoryQuery = pm.newQuery(Person.class);
		attendingHistoryQuery
				.setFilter("name == nameParam && prename == prenameParam");
		attendingHistoryQuery
				.declareParameters("String nameParam, String prenameParam");

		List<Person> persons = (List<Person>) attendingHistoryQuery.execute(
				name[0], name[1]);
		List<EventStatus> eventStatusList = null;
		List<Event> eventList = null;

		if (!persons.isEmpty()) {
			person = persons.get(0);
			eventStatusList = person.getEvents();
		}

		// Query for the Event
		Query eventQuery = pm.newQuery(Event.class);
		Date date = new Date();

		// Events in the future or past ones?
		if (history.equals("true")) {
			eventQuery.setFilter("date <= dateParam");
		} else {
			eventQuery.setFilter("date > dateParam");
		}
		eventQuery.declareParameters("Date dateParam");
		eventQuery.setOrdering("date descending");
		eventQuery.declareImports("import java.util.Date");
		eventList = (List<Event>) eventQuery.execute(date);

		if (person != null) {
			if (eventList != null && eventStatusList != null) {
				if (!(eventList.isEmpty() && eventStatusList.isEmpty())) {
					List<Event> toJson = new ArrayList<Event>();

					for (EventStatus es : eventStatusList) {
						for (Event e : eventList) {
							if (es.getEvent().equals(e.getTitle())
									&& es.getStatus().equals(status)) {
								toJson.add(e);
							}
						}
					}

					Gson gson = new Gson();

					// Set the corret response type
					resp.getWriter().println(
							URLEncoder.encode(gson.toJson(toJson), "UTF-8"));
					resp.setStatus(200);
				}
			}
		}
	}
}
